<?php

/**
* 休市安排
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Holiday  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
	* 休市安排列表
	*/
	public function index($title = 0, $p = 1){
		//接收参数
		$p = intval($p);
		$title = urldecode(sql_format($title));
		
		//通用搜索
		$where = " where 1=1";
		
		//标题搜索
		$title = sql_format($title);
		if($title){
			$where .= " AND title like  '%$title%'";
		} else {
			$title = 0;
		}

		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(*) as c from config_holiday" . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];

		//分页查询
		$sql = "select *  from config_holiday" . $where . " order by id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . "/" . $this->_action  . "/" . $title . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("title", $title);

		$this->display();
	}
	
	/**
	* 新增休市安排
	* @param    int  	$id   			id
	* @param    char  	$title   		标题
	* @param    int  	$begin_date   	开始日期
	* @param    int  	$end_date   	结束日期
	* @param    char  	$description   	备注说明
	*/
	public function add(){
		if(IS_POST){
			//接收参数
			$title = isset($_POST["title"]) ? substr(sql_format($_POST["title"]), 0, 50) : "";
			$begin_date =  isset($_POST["begin_date"]) ? $_POST["begin_date"] : "0";
			$end_date =  isset($_POST["end_date"]) ? $_POST["end_date"] : "0";
			$description = isset($_POST["description"]) ? substr(sql_format($_POST["description"]), 0, 200) : "";
			$is_storagefee = isset($_POST["is_storagefee"]) ? $_POST["is_storagefee"] : 0;
			//校验参数
			if(! $title){
				ajax_return(0, "请输入标题");
			}
			if(! $begin_date){
				ajax_return(0, "请输入开始日期");
			}
			if(! strtotime( $begin_date) ){
				ajax_return(0, "开始日期格式错误");
			}
			if(! $end_date){
				ajax_return(0, "请输入结束日期");
			}
			if(! strtotime( $end_date) ){
				ajax_return(0, "结束日期格式错误");
			}
			if( strtotime( $end_date) < strtotime( $begin_date) ){
				ajax_return(0, "结束日期不能小于开始日期");
			}

			$sql = "select id from config_holiday where title = '$title'";
			$result = $this->db->query($sql)->row_array();
			$result && ajax_return(0, "标题已经存在");
			
			$sql = "insert into config_holiday (title,begin_date,end_date,is_storagefee,description) values ('$title','" . strtotime($begin_date) ."','" . strtotime($end_date) ."',".$is_storagefee.",'$description')";
			$this->db->query($sql);
			$res =$this->db->insert_id();
			if(! $res){
				ajax_return( 0, "添加失败" );
			} else{
				ajax_return( 1, "添加成功" , "/Holiday" );
			}
		}
		
		$this->display();
	}
	
	/**
	* 编辑休市安排
	* @param    int  	$id   			id
	* @param    char  	$title   		标题
	* @param    int  	$begin_date   	开始日期
	* @param    int  	$end_date   	结束日期
	* @param    char  	$description   	备注说明
	*/
	public function edit($id = 0){
		//接收参数
		$id = intval($id);
		
		if(IS_POST){
			if(0 >= $id){
				ajax_return( 0 , "参数有误" );
			}
			$title = isset($_POST["title"]) ? substr(sql_format($_POST["title"]), 0, 50) : "";
			$begin_date =  isset($_POST["begin_date"]) ? $_POST["begin_date"] : "0";
			$end_date =  isset($_POST["end_date"]) ? $_POST["end_date"] : "0";
			$description = isset($_POST["description"]) ? substr(sql_format($_POST["description"]), 0, 200) : "";
			$is_storagefee = isset($_POST["is_storagefee"]) ? $_POST["is_storagefee"] : 0;

			if(! $title){
				ajax_return(0, "请输入标题");
			}
			if(! $begin_date){
				ajax_return(0, "请输入开始日期");
			}
			if(! strtotime( $begin_date) ){
				ajax_return(0, "开始日期格式错误");
			}
			if(! $end_date){
				ajax_return(0, "请输入结束日期");
			}
			if(! strtotime( $end_date) ){
				ajax_return(0, "结束日期格式错误");
			}
			if( strtotime( $end_date) < strtotime( $begin_date) ){
				ajax_return(0, "结束日期不能小于开始日期");
			}
			
			$sql = "select id from config_holiday where id<>$id   AND title = '$title'";
			$result = $this->db->query($sql)->row_array();
			$result && ajax_return(0, "标题已经存在");
			
			$sql = "update config_holiday set title = '$title',begin_date = '" . strtotime($begin_date) . "',end_date = '" . strtotime($end_date) . "',is_storagefee=".$is_storagefee.",description = '$description' where id = $id";
			$this->db->query($sql);
			
			//返回结果集
			ajax_return( 1, "更新成功" , "/Holiday" );
		} else{
			if(0 >= $id){
				$this->error("参数有误");
			}
			$sql = "select * from config_holiday where id = $id";
			$result = $this->db->query($sql)->row_array();
			if(! $result){
				$this->error("记录不存在");
			}
			
			$this->assign("result", $result);
			$this->display();
		}
	}
	
	/**
	* 删除休市安排
	* @param    int  	$id   			id
	*/
	public function delete($id = 0){
		if(IS_POST){
			!$id &&	ajax_return(0, "参数有误");
			$sql = "delete from  config_holiday where id in ($id)";
			$this->db->query($sql);
			ajax_return(1, "删除成功");
		}
	}
}
